<?php
namespace ZHMVC\D;

class Module extends \ZHMVC\D\DataBase
{
    public function getAllNum($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select count(*) as num from zhmvc_module";
                $data=$this->_db->getOne($sql);
            }
            else
            {
                //去掉第一个逻辑符号
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                
                $sql="select count(*) as num from zhmvc_module where ".$tempS;
                $data=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select count(*) as num from zhmvc_module where ".$parameter;
            $data=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select count(*) as num from zhmvc_module";
            $data=$this->_db->getOne($sql);
        }
        if(empty($data)==true)
        {
            $data["num"]=0;
        }
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getAll($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from zhmvc_module";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_module where ".$tempS;
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_module where ".$parameter."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from zhmvc_module";
            $datas=$this->_db->getAll($sql);
        }
        
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function getRows()
    {
        return $this->_rows;
    }
    
    public function getPages($limit,$parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            if($s=="")
            {
                $sql="select * from zhmvc_module order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_module where ".$tempS." order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_module where ".$parameter." order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from zhmvc_module order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function getOne($postid)
    {
        $sql="select * from zhmvc_module where id=:id";
        $bind=array(":id"=>$postid);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getOne1($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from zhmvc_module";
                $datas=$this->_db->getOne($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_module where ".$tempS;
                $datas=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_module where ".$parameter."";
            $datas=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select * from zhmvc_module";
            $datas=$this->_db->getOne($sql);
        }
        
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function  getOneByModulename($modulename)
    {
        $sql="select * from " . $this->_pre . "module where modulename=:modulename";
        $bind=array(":modulename"=>$modulename);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function add($biaoqianzhui, $mulu, $name, $modulename, $version="1.0",$rpctype = '本地', $rpcurl = '', $rpcmainkey = '',$rpchost='',$rpcprivatekey='',$rpcmoduleid=1)
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        
        $sql = "insert into " . $this->_pre . "module(id,biaoqianzhui,mulu,name,modulename,modulenamespace,rpctype,rpcurl,rpcmainkey,rpchost,rpcprivatekey,rpcmoduleid,`version`) values ('".$id."',:biaoqianzhui,:mulu,:name,:modulename,:modulenamespace,:rpctype,:rpcurl,:rpcmainkey,:rpchost,:rpcprivatekey,:rpcmoduleid,:version)";
        //echo $sql;
        $bind = array(
            ":biaoqianzhui" => $biaoqianzhui,
            ":mulu" => $mulu,
            ":name" => $name,
            ":modulename" => $modulename,
            ":modulenamespace" => strtoupper($modulename),
            ":rpctype" => $rpctype,
            ":rpcurl" => $rpcurl,
            ":rpcmainkey" => $rpcmainkey,
            ":rpchost" => $rpchost,
            ":rpcprivatekey" => $rpcprivatekey,
            ":rpcmoduleid" => $rpcmoduleid,
            ":version" => $version
        );
        //print_r($bind);
        $this->_db->update($sql, $bind);
        $this->_lastid = $id;
        return 1;
    }

    public function update($id, $biaoqianzhui, $mulu, $name, $modulename, $rpctype = '本地', $rpcurl = '', $rpcmainkey = '',$rpchost='',$rpcprivatekey='',$rpcmoduleid=1)
    {
        $sql = "update " . $this->_pre . "module set biaoqianzhui=:biaoqianzhui,mulu=:mulu,name=:name,modulename=:modulename,rpctype=:rpctype,rpcurl=:rpcurl,rpcmainkey=:rpcmainkey,rpchost=:rpchost,rpcprivatekey=:rpcprivatekey,rpcmoduleid=:rpcmoduleid where id=:id";
        $bind = array(
            ":biaoqianzhui" => $biaoqianzhui,
            ":mulu" => $mulu,
            ":name" => $name,
            ":modulename" => $modulename,
            ":rpctype" => $rpctype,
            ":rpcurl" => $rpcurl,
            ":rpcmainkey" => $rpcmainkey,
            ":rpchost" => $rpchost,
            ":rpcprivatekey" => $rpcprivatekey,
            ":rpcmoduleid" => $rpcmoduleid,
            ":id" => $id
        );
        $this->_db->update($sql, $bind);
        return 1;
    }

    public function delete($id)
    {
        $sql = "delete from " . $this->_pre . "module where id=:id";
        $bind = array(
            ":id" => $id
        );
        $this->_db->update($sql, $bind);
        return 1;
    }
    
    public function addOne()
    {

        $sql = "insert into " . $this->_pre . "module(`name`) values (:name)";
        //echo $sql;
        $bind = array(
            ":name" => $name
        );
        //print_r($bind);
        $this->_db->update($sql, $bind);
        $this->_lastid = $this->_db->getLastId();
        return 1;
    }

    public function getLastId()
    {
        return $this->_lastid;
    }
}